package com.pw.study.tcp;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;

/**
 * @Author: linux_future
 * @since: 2022/3/24
 **/
public class TcpClientV extends Frame {
    private final TextArea textArea = new TextArea();
    private final TextField textField = new TextField();
    private DataOutputStream dataOutputStream = null;
    private DataInputStream dataInputStream = null;
    private Socket socket = null;
    private boolean isConnected = false;
    private Thread thread = null;

    public static void main(String[] args) {
        new TcpClientV().frameClient();
    }

    //聊天窗口
    public void frameClient() {
        setSize(400, 400);
        setLocation(400, 300);
        add(textArea, BorderLayout.NORTH);
        add(textField, BorderLayout.SOUTH);
        pack();
        textField.addActionListener(new TfListener());
        //关闭窗口事件监听
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                disconnected();
                System.exit(0);
            }
        });
        this.connect();
        setVisible(true);
    }

    //链接服务器地址
    private void connect() {
        try {
            socket = new Socket("localhost", 8888);
            thread = new Thread(new ChatThread());
            thread.start();
            dataInputStream = new DataInputStream(socket.getInputStream());
            dataOutputStream = new DataOutputStream(socket.getOutputStream());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //断开连接
    private void disconnected() {
        isConnected = false;
        try {
            dataOutputStream.close();
            dataInputStream.close();
            socket.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

    //键盘回车事件
    private class TfListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            String strMsg = textField.getText();
            System.out.println("---------------");
            textField.setText("");
            try {
                dataOutputStream.writeUTF(strMsg);
                dataOutputStream.flush();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }

    }

    //开启线程接受服务器信息
    private class ChatThread implements Runnable {
        @Override
        public void run() {

            try {
                dataInputStream = new DataInputStream(socket.getInputStream());
                dataOutputStream = new DataOutputStream(socket.getOutputStream());
                isConnected = true;
                while (isConnected) {
                    String msg = dataInputStream.readUTF();
                    String taText = textArea.getText();
                    textArea.setText(taText + msg + "\n");

                }
            } catch (SocketException e) {
                System.out.println("客户端退出");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
